package org.teamh.connection.handler;

import org.teamh.game.Start;
import org.teamh.logic.lobby.Map;

import de.lessvoid.nifty.controls.ListBox;

public class MapHandler extends MasterHandler {
	
	public MapHandler(Start start) {
		super(start);
	}
	
	public boolean handleMsg(String line) {
		if (line.startsWith("MAP")) {
			
			String name = "";
			String creator = "";
			int version = 0;
			boolean duplicate = false;
			
			String[] str = line.split(" ");
			
			if(str.length < 3) {
				return false;
			}
			
			name = str[1].substring(5);
			creator = str[2].substring(8);
			version = Integer.parseInt(str[3].substring(8));
			
			Map map = new Map(name, creator, version);
			for (Map m : start.getModel().getMaps()) {
				if (m.getName().equals(name)) {
					duplicate = true;
				}
			}
			
			if (!duplicate) {
				start.getModel().addMap(map);
			}
			
			if (start.getGui().getNifty().getCurrentScreen().getScreenId().contains("mapeditor")) {
				@SuppressWarnings("unchecked")
				ListBox<String> box = start.getGui().getMapEditorScreen().getDownloadMapPopup().findNiftyControl("mapdownloadlist", ListBox.class);
				box.addItem(name);
			}	
			return true;
		}
		return false;
	}
}
